package com.yunji.aggregate.service;

import com.yunji.aggregate.convert.AggregateParamConvert;
import com.yunji.aggregate.mapper.IProductAggregateMapper;
import com.yunji.aggregate.model.dto.analysis.LinkageQueryParam;
import com.yunji.aggregate.model.dto.analysis.ProductOrderStatisticsParam;
import com.yunji.aggregate.model.dto.order.StatsOrdersParams;
import com.yunji.aggregate.model.dto.product.PricingParam;
import com.yunji.aggregate.model.dto.product.ProductDTO;
import com.yunji.aggregate.model.vo.analysis.LinkageQueryVO;
import com.yunji.aggregate.model.vo.product.PricingProductStatisticsVO;
import com.yunji.aggregate.model.vo.product.ProductOrderStatisticsVO;
import com.yunji.aggregate.model.vo.product.ProductStatisticsVO;
import com.yunji.common.constant.LogicCode;
import com.yunji.common.enums.common.UserType;
import com.yunji.common.model.dto.LoginInfo;
import com.yunji.common.utils.bean.BeanUtils;
import com.yunji.system.context.SystemContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

@Service
@Slf4j
public class ProductAggregateService {

    @Resource
    private IProductAggregateMapper aggregateMapper;

    public List<ProductStatisticsVO> searchProductList(ProductDTO dto) {
        LoginInfo user = SystemContext.getUser();
        Long belongId = user.getBelongId();
        dto.setBelongId(belongId);
        dto.setAccreditStatus(LogicCode.NO);
        String userType = user.getUserType();
        if (UserType.AGENT.getCode().equals(userType)) {
            dto.setBParentId(belongId);
        }
        if (UserType.SITE.getCode().equals(userType)) {
            dto.setBBelongId(belongId);
        }
        return aggregateMapper.searchProductList(dto);
    }

    public List<PricingProductStatisticsVO> searchAgentPrice(PricingParam param) {
        LoginInfo user = SystemContext.getUser();
        Long belongId = user.getBelongId();
        String userType = user.getUserType();
        StatsOrdersParams statsParams = new StatsOrdersParams();
        if (UserType.AGENT.getCode().equals(userType)) {
            param.setParentId(belongId);
        }
        if (UserType.SITE.getCode().equals(userType)) {
            param.setBelongId(belongId);
        }
        BeanUtils.clone(param, statsParams);
        return aggregateMapper.searchPricingList(param);
    }

    public List<String> searchAgentName(String agentName) {
        Assert.isTrue((agentName.length() <= 15), "输入的代理商名称不合规");
        List<String> list = aggregateMapper.searchBarAgentName(agentName);
        return BeanUtils.isNotNull(list) ? list : Collections.emptyList();
    }

    public List<String> searchManagerName(String managerName) {
        Assert.isTrue((managerName.length() <= 15), "输入的代理商名称不合规");
        LoginInfo user = SystemContext.getUser();
        Long belongId = user.getBelongId();
        String userType = user.getUserType();
        PricingParam param = new PricingParam();
        param.setSiteName(managerName);
        if (BeanUtils.equals(UserType.AGENT.getCode(), userType)) {
            param.setBelongId(belongId);
        }
        List<String> list = aggregateMapper.searchBarManagerName(param);
        return BeanUtils.isNotNull(list) ? list : Collections.emptyList();
    }

    public List<ProductOrderStatisticsVO> productSalesStats(ProductOrderStatisticsParam param) {
        return aggregateMapper.productSalesStats(param);
    }

    public List<LinkageQueryVO> productLinkageQuery(LinkageQueryParam param) {
        AggregateParamConvert.handleLinkageQuery(param);
        return aggregateMapper.productLinkageQuery(param);
    }

    public List<ProductStatisticsVO> agentSearch(ProductDTO dto) {
        dto.setUserType(UserType.AGENT.getCode());
        dto.setAccreditStatus(LogicCode.NO);
        List<ProductStatisticsVO> list = aggregateMapper.searchProductList(dto);
        StatsOrdersParams params = AggregateParamConvert.currentUser();
        BeanUtils.clone(dto, params);
//        List<StatsOrdersVO> statsList = aggregateMapper.statsOfOrdersByProduct(params);
//        Map<Long, Integer> map = statsList.stream().collect(Collectors.toMap(StatsOrdersVO::getProductId, StatsOrdersVO::getNums, (k1, k2) -> k1));
//        list.forEach(p -> {
//            Long productId = p.getId();
//            Integer nums = map.getOrDefault(productId, 0);
//            p.setSale(Long.valueOf(nums));
//        });
        return list;
    }
}
